define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user_coupons/index' + location.search,
                    add_url: 'user_coupons/add',
                    multi_url: 'user_coupons/multi',
                    import_url: 'user_coupons/import',
                    table: 'user_coupons',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                showExport: false,
                showToggle: false,
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), searchable: false},
                        {field: 'user.nickname',operate: 'LIKE', title: __('User_id'), width: '200',formatter:function(v,row){
                                var html = '<div style="display:flex"><img width="60px" height="60px" src="'+Fast.api.cdnurl(row.user.avatar)+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.user.id+'<br/>'+row.user.nickname+'<br/>'+row.user.mobile+'</p></div>';
                                return html;
                            }
                        },
                        {field: 'buyed', title: __('Buyed')},
                        {field: 'nums', title: __('Nums')},
                        {field: 'createtime', title: __('初次购买时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('最新购买时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {
                                    name: 'log',
                                    text: '日志',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa fa-list',
                                    extend: 'data-area=\'["70%", "80%"]\'',
                                    url: function (row) {
                                        return `user_coupons_log/index?user_id=${row.user_id}`;
                                    }
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
